from flask import *
import os
from src import utils, log

blueprint = Blueprint('common', __name__)


@blueprint.route('/api/file/upload', methods=['POST'])
def upload():
    try:
        f = request.files['file']
        upload_path = utils.get_upload_path()
        utils.is_exists(upload_path)
        new_file_name = utils.get_uuid() + '.' + f.filename.split('.')[1]
        upload_path = os.path.join(upload_path + '/' + new_file_name)
        f.save(upload_path)
        return jsonify({'code': 200, 'message': '上传成功', 'success': True, 'data': new_file_name})

    except Exception as e:
        log.error(e)
        return jsonify({'code': -1, 'message': '上传失败', 'success': False, 'data': None})
